package com.kgc.fk.Controller;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.fk.Feign.CwCGDDConsumerFeign;
import com.kgc.fk.Service.BuyPayServ;
import com.kgc.fk.Service.PrePayServ;
import com.kgc.fk.Service.PublicConServ;
import com.kgc.fk.Service.RefundServ;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("TKD")
public class RefundController {

    Logger logger = LoggerFactory.getLogger(RefundController.class);

    @Resource
    private RefundServ refundServ;

    @Resource
    private PublicConServ publicConServ;
    @Resource
    private CwWorkFlowFeign cwWorkFlowFeign;

    @Resource
    private BuyPayServ buyPayServ;
    @Resource
    private PrePayServ prePayServ;
    @Resource
    private CwCGDDConsumerFeign cwCGDDConsumerFeign;

    @RequestMapping("getRefundType")
    public ResponseMsg getRefundType(){
        ResponseMsg responseMsg = refundServ.getRefundType();
        return responseMsg;
    }

    @RequestMapping("getPage")
    public ResponseMsg getPage(@RequestBody Map<String, Object> params){
        ResponseMsg responseMsg = refundServ.getRefundPage(params);
        return responseMsg;
    }
    @RequestMapping("delRefund")
    public ResponseMsg delRefund(String code){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("del",1);
        params.put("code",code);
        ResponseMsg responseMsg = refundServ.updateRefund(params);
        return responseMsg;
    }

    @RequestMapping("addRefund")
    public ResponseMsg addRefund(@RequestBody Map<String, Object> params){
        String code = "TKD-" +  publicConServ.getCode("cw_fk_refund");
        params.put("code", code);
        ResponseMsg responseMsg = refundServ.addRefund(params);
        
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",code);//单据编号
        variables.put("type",params.get("type"));//流程类型
        //创建一个流程实例
        com.kgc.activiti.util.ResponseMsg res = cwWorkFlowFeign.startProcess(variables);
        if (!"200".equals(res.getCode())){
            responseMsg.setCode(res.getCode());
        }
        return responseMsg;
    }
    @RequestMapping("getRefund")
    public ResponseMsg getRefund(@RequestBody Map<String, Object> params){
        ResponseMsg responseMsg = refundServ.getRefund(params);
        return responseMsg;
    }

    @RequestMapping("modiRefund")
    public ResponseMsg modiRefund(@RequestBody Map<String, Object> params){
        ResponseMsg responseMsg = refundServ.modiRefund(params);
        return responseMsg;
    }

    @RequestMapping("/check")
    public  ResponseMsg check(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = new ResponseMsg();
        logger.info("Refund check params,{}",map.toString());

        Map<String,Object> refund = (Map<String, Object>) refundServ.getRefund(map).getData();

        Map<String , Object> params = new HashMap<>();
        params.put("code",refund.get("sourceCode"));
        Map<String , Object> sourceOrder = new HashMap<>();
        String type = refund.get("sourceId").toString();
        switch (type){
            case "1":
                sourceOrder = (Map<String, Object>) prePayServ.getPrePay(params).getData();
                //预付款单

                double prePay = Double.parseDouble(refund.get("totalAmount").toString());
//                ResponseMsg cgdd = cwCGDDConsumerFeign.getCGDD(refund.get("sourceCode").toString());
                params.clear();
                params.put("code", sourceOrder.get("sourceSheet"));
                Double maxAmount = (Double) cwCGDDConsumerFeign.getPrePayRefundAmount(params).getData();
//                ResponseMsg getMoney = prePayServ.calPrePayRefund(refund.get("sourceCode").toString());

                if (prePay > maxAmount){
                    responseMsg.setCode("300");
                    responseMsg.setMsg("退款金额超出最大退款金额金额");
                    return responseMsg;
                }
                break;
            case "2":
//                sourceOrder = (Map<String, Object>) buyPayServ.getBuyPay(params).getData();
                sourceOrder.put("status",1);
                break;
            case "3":
                sourceOrder = (Map<String, Object>) buyPayServ.getBuyPay(params).getData();
                break;
            case "4":
                sourceOrder.put("status",1);
                break;
        }
        if ("11".equals(sourceOrder.get("status").toString())){
            responseMsg.setCode("300");
            responseMsg.setMsg("源单已关闭");
            return responseMsg;
        }




        Map<String,Object> variables = new HashMap<>();
        variables.put("code",map.get("code"));//单据编号
        variables.put("checkManId",Integer.valueOf((String) map.get("checkManId")));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        return responseMsg;
    }
}
